<?php

namespace app\common\controller;

use app\common\model\AdminDepartment;
use app\common\model\AdminMenu;
use app\common\model\AdminPower;
use app\common\model\AdminRole;
use app\common\model\AdminUser;
use app\common\model\RuinuoApp;
use app\common\model\RuinuoButton;
use app\common\model\RuinuoMode;
use think\facade\Request;

class Power
{
    public function Index()
    {
        return ViewTpl('common/power/index');
    }

    public function Mark()
    {
        if (Request::isAjax() || Request::isPost()) {
            $type = input("type") ?: 'role';
            $rows = [];
            switch ($type) {
                case "role":
                    $rows = AdminRole::field("id,name,num")->select()->toArray();
                    break;
                case "user":
                    $rows = AdminUser::field("id,name,user as num")->select()->toArray();
                    break;
                case "department":
                    $rows = AdminDepartment::field("id,name,num,parent_id")->select()->toArray();
                    break;
            }
            $data['rows'] = $rows;
            $data['total'] = count($rows);
            return BackCode($data);
        }
    }


    public function Cat()
    {
        if (Request::isAjax() || Request::isPost()) {
            $type = input("type") ?: 'menu';
            $mark = input("mark") ?: 'mark';
            $mark_id = input("mark_id") ?: 'mark_id';
            $find = AdminPower::where("cat='" . $type . "' and mark='" . $mark . "' and mark_id='" . $mark_id . "'")->field('cat_ids')->findOrEmpty()->toArray();
            $cat_ids = !empty($find['cat_ids']) ? $find['cat_ids'] : '';
            $cat_ids = explode(',', $cat_ids);
            $ids = '';
            foreach ($cat_ids as $k => $v) {
                $ids.= '[' . $v . ']';
            }
            $rows = [];
            switch ($type) {
                case "menu":
                    $rows = AdminMenu::field("id,title as name,parent_id")->select()->toArray();
                    foreach ($rows as $k => $v) {
                        $rows[$k]['num'] = '-';
                        $LAY_CHECKED = false;
                        if (strpos($ids, '[' . $v['id'] . ']') !== false) {
                            $LAY_CHECKED = true;
                        }
                        $rows[$k]['LAY_CHECKED'] = $LAY_CHECKED;
                    }
                    break;
                case "button":
                    $app = RuinuoApp::field("id,name,num")->select()->toArray();
                    foreach ($app as $k => $v) {
                        $rows[] = [
                            "id" => 'app_' . $v['id'],
                            "name" => $v['name'],
                            "num" => $v['num'],
                            "parent_id" => 0,
                        ];
                        $mode = RuinuoMode::where("app_id='" . $v['id'] . "'")->field("id,name,num")->select()->toArray();
                        foreach ($mode as $k2 => $v2) {
                            $rows[] = [
                                "id" => 'mode_' . $v2['id'],
                                "name" => $v2['name'],
                                "num" => $v2['num'],
                                "parent_id" => 'app_' . $v['id'],
                            ];
                            $Button = RuinuoButton::where("mode_id='" . $v2['id'] . "'")->field("id,title")->select()->toArray();
                            foreach ($Button as $k3 => $v3) {
                                $LAY_CHECKED = false;
                                if (strpos($ids, '[' . $v3['id'] . ']') !== false) {
                                    $LAY_CHECKED = true;
                                }
                                $rows[] = [
                                    "id" => $v3['id'],
                                    "name" => $v3['title'],
                                    "num" => '-',
                                    "parent_id" => 'mode_' . $v2['id'],
                                    "LAY_CHECKED" => $LAY_CHECKED
                                ];
                            }
                        }
                    }

                    break;
            }
            $data['rows'] = $rows;
            $data['total'] = count($rows);
            return BackCode($data);
        }
    }

    public function Edit()
    {
        if (Request::isAjax() || Request::isPost()) {
            $AdminPower = new AdminPower();
            $mark = input("mark");
            if (empty($mark)) {
                return BackCode([], 203, 'mark参数不能为空');
            }
            $mark_id = input("mark_id");
            if (empty($mark_id)) {
                return BackCode([], 203, 'mark_id参数不能为空');
            }
            $cat = input("cat");
            if (empty($cat)) {
                return BackCode([], 203, 'cat参数不能为空');
            }
            $find = $AdminPower->where("cat='" . $cat . "' and mark='" . $mark . "' and mark_id='" . $mark_id . "'")->field('id')->findOrEmpty()->toArray();
            $upData = [
                'mark' => $mark,
                'mark_id' => $mark_id,
                'cat' => $cat,
                'cat_ids' => input("cat_ids"),
                'update_time' => date('Y-m-d H:i:s'),
                'update_id' => GetToken('id'),
            ];
            if (empty($find)) {
                $upData['create_time'] = $upData['update_time'];
                $upData['create_id'] = $upData['update_id'];
                $AdminPower->insert($upData);
            } else {
                $AdminPower->where("id='" . $find['id'] . "'")->update($upData);
            }
            return BackCode();
        }
    }
}